//
//  PrescriptionOrderDetailCellViewModel.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2021/8/11.
//  Copyright © 2021 QDSG. All rights reserved.
//

import Foundation

class PrescriptionOrderDetailCellViewModel: NSObject {
    var model: PrescriptionOrderDetailSection
    
    init(with model: PrescriptionOrderDetailSection) {
        self.model = model
    }
    
    var mode: PrescriptionOrderDetailSectionMode { model.mode }
    
    var receiveType: PrescriptionOrderReceiveType {
        get { model.orderDetail.receiveType }
        set { model.orderDetail.receiveType = newValue }
    }
    
    var addressClickEnable: Bool {
        receiveType == .express && model.orderDetail.status == .waitPay
    }
    
    var orderId: String? { model.orderDetail.orderId }
    var medicalId: String { "病历号：\(model.orderDetail.medicalId ?? "")" }
    var prescriptionId: String { "处方编号：\(model.orderDetail.preId ?? "")" }
    var backWaybillNo: String? { model.orderDetail.backWaybillNo }
    
    var patientName: String { model.orderDetail.patientName ?? "" }
    var patientDetailName: String { "就诊人：\(patientName)" }
    var patientSex: String { "性别：\(model.orderDetail.patientSex ?? "")" }
    var patientAge: String { "年龄：\(model.orderDetail.patientAge ?? "")岁" }

    
    
    /// 科室
    var openDoctorDept : String { "就诊科室：\(model.orderDetail.openDoctorDept ?? "")" }
    /// 配药时间
    var compoundTime : String { model.orderDetail.compoundTime ?? "" }
    /// 发药时间
    var distributeTime : String { model.orderDetail.distributeTime ?? "" }

    /// 审核时间
    var passTime : String { model.orderDetail.passTime ?? "" }
    /// 身份证
    var patientIdcard : String {  "身份证：\(model.orderDetail.patientIdcard ?? "")" }
    
    var patientSexAndAge: String {
        "\(model.orderDetail.patientSex ?? "") \(model.orderDetail.patientAge ?? "")岁"
    }
    
    var mainDiagnoseResult: String {
        "\(model.orderDetail.preAppend ?? "") - \(model.orderDetail.icd ?? "") - \(model.orderDetail.tailAppend ?? "")"
    }
    
    var diagnoseAdvice: String? { model.orderDetail.diagnoseAdvice }
    
    var drugAdvice: String? { model.orderDetail.remark }
    
    var receiverName: String { model.orderDetail.receiverName ?? "" }
    var receiverPhone: String { model.orderDetail.receiverPhone ?? "" }
    var receiverAddress: String { model.orderDetail.receiverAddress ?? "" }
    
    var hospitalName: String { model.orderDetail.hospitalName ?? "" }
    var hospitalPhone: String { model.orderDetail.hospitalPhone ?? "" }
    var hospitalAddress: String { model.orderDetail.hospitalAddress ?? "" }
    
    var openDoctorSignImg: String {model.orderDetail.openDoctorSignImg ?? ""}
    var auditorSignImg: String {model.orderDetail.auditorSignImg ?? ""}
    
    var totalPrice: String { "合计：\(model.orderDetail.prescriptionAmount ?? "")" } 
    
    var doctorName: String? { model.orderDetail.doctorName }
    var makeTime: String? { model.orderDetail.preTime }
    
    var evolutionDoctorName: String { "开方医师：\(model.orderDetail.doctorName ?? "")" }
    var deployDoctorName: String { "调配：\(model.orderDetail.compoundDoctorName ?? "")" }
    var sendDoctorName: String { "发药：\(model.orderDetail.distributeDoctorName ?? "")" }
    
    var status: PrescriptionOrderStatus? { model.orderDetail.status }
    var statusName: String? { model.orderDetail.statusName }
    
    var address: String {
        switch receiveType {
        case .express:  return receiverAddress
        case .hospital: return hospitalName
        default:        return ""
        }
    }
    
    var emsFee: String { "配送邮费（预估\(model.orderDetail.emsFee)元）将在收货时到付" }
    
    var createTime: String? { model.orderDetail.createTime }
    
    var payTime: String? { model.orderDetail.payTime }
    var receiveTime: String? { model.orderDetail.receiveTime }
    var applyBackTime: String? { model.orderDetail.applyBackTime }
    var backFinishTime: String? { model.orderDetail.backFinishTime }
    var cancelTime: String? { model.orderDetail.cancelTime }
    var deliveryTime: String? { model.orderDetail.deliveryTime }
    var rejectBack: String? { model.orderDetail.rejectBack }
    
    var numberOfRows: Int {
        switch mode {
        case .orderStatus:
            guard let status = status else { return 0 }
            switch status {
            case .waitPay: return 1
            case .waitShip: return 5
            case .shipped,
                 .applyRefund,
                 .waitSendOut,
                 .waitShipApplyRefund,
                 .shippedApplyRefund,
                 .completeApplyRefund,
                 .waitTakeApplyRefund,
                 .waitSendOutUnreceived,
                 .waitSendOutReceived: return 6
            case .reject,
                 .completed,
                 .sendingBack: return 7
            case .waitTake: return 5
            case .refunded: return receiveType == .express ? 8 : 7
            case .canceled: return 2
            default:        return 0
            }
        default:     return 1
        }
    }
    
    var westernItems: [DrugsRowViewModel] {
        let westernList = model.orderDetail.westernNameDtoList ?? []
        return westernList.map {
            var drugRowVm = DrugsRowViewModel(model: $0)
            drugRowVm.drugFrequency = drugRowVm.drugUsing
            return drugRowVm
        }
    }
    
    var cellHeight: CGFloat {
        switch mode {
        case .doctorInfoForDetail:
            return 170
        case .receiveType: return 90.0
        case .address,
             .doctorInfoForPay:     return 70.0
        case .patientInfoForPay,
             .drugAdvice,
             .medicalRecordNo,
             .orderStatus:          return UITableView.automaticDimension
        case .drugs:
            let defaultHeight: CGFloat = 40 + 20 + 44
            return westernItems.isEmpty
                ? defaultHeight + 50
                : defaultHeight + 50 * CGFloat(westernItems.count)
        case .patientInfoForDetail: return 210.00
        }
    }
    
    var headerHeight: CGFloat {
        switch mode {
        case .receiveType,
             .address,
             .patientInfoForPay,
             .patientInfoForDetail,
             .medicalRecordNo:      return 16
        case .orderStatus:          return 66
        default:                    return 0.001
        }
    }
}
